#
# test_automaton.rb - automaton correctness test case
#

$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'test/unit'
require 'Automaton'
require 'Optimizer'

class TestAutomaton < Test::Unit::TestCase
  def test_simpleregexp
    
    expected_ic = [
      '#', 115, '#', '#', 105, '#', '#', 109,
      '#', '#', 112, '#', '#', 108, '#', '#',
      101
    ]
    
    expected_state1 = [
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
    ]

    expected_state2 = [
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
    ]
    
    automa = Automaton.new("simple")
    first_state = automa.espressione()
    
    ic = automa.ic
    state1 = automa.state1
    state2 = automa.state2
    
    assert_equal(0, first_state, "While testing first state : ")
    assert_equal(17, ic.length, "While testing token array size : ")
    assert_equal(17, state1.length, "While testing state1 array size : ")
    assert_equal(17, state2.length, "While testing state2 array size : ")
    assert_equal(expected_ic, ic, "While testing token array contents : ")
    assert_equal(expected_state1, state1, "While testing state1 array contents : ")
    assert_equal(expected_state2, state2, "While testing state2 array contents : ")
    
    # assert_equal("foo", bar)
  end
  
  def test_optimized_simpleregexp

    automa = Automaton.new("simple")
    first_state = automa.espressione()
    
    ic = automa.ic
    state1 = automa.state1
    state2 = automa.state2
    
    ottimizzazione = Optimizer.new(ic, state1, state2, first_state)
    ottimizzazione.ottimizza 
    ic = ottimizzazione.ic
    state1 = ottimizzazione.state1
    state2 = ottimizzazione.state2
    first_state = ottimizzazione.first_state
  
    assert_equal(0, first_state, "While testing first state : ")
    assert_equal(12, ic.length, "While testing token array size : ")
    assert_equal(12, state1.length, "While testing state1 array size : ")
    assert_equal(12, state2.length, "While testing state2 array size : ")
  end
  
  def test_star_regexp
    
    automa = Automaton.new("a*")
    
    first_state = automa.espressione()
    ic = automa.ic
    state1 = automa.state1
    state2 = automa.state2

    expected_ic = ['#',97,'#','#','#']
    expected_state1 = [1,2,3,5,3]
    expected_state2 = [1,2,3,0,3]
    expected_first_state = 4
    expected_length = 5
    
    assert_equal(expected_first_state, first_state, "While testing first state : ")
    assert_equal(expected_length, ic.length, "While testing token array size : ")
    assert_equal(expected_length, state1.length, "While testing state1 array size : ")
    assert_equal(expected_length, state2.length, "While testing state2 array size : ")
    assert_equal(expected_ic, ic, "While testing token array contents : ")
    assert_equal(expected_state1, state1, "While testing state1 array contents : ")
    assert_equal(expected_state2, state2, "While testing state2 array contents : ")
   
    ottimizzazione = Optimizer.new(ic, state1, state2, first_state)
    ottimizzazione.ottimizza
    
    opt_first_state = ottimizzazione.first_state
    opt_ic = ottimizzazione.ic
    opt_state1 = ottimizzazione.state1
    opt_state2 = ottimizzazione.state2
    
    expected_opt_ic = ['#',97,'#','#']
    expected_opt_state1 = [1,2,4,2]
    expected_opt_state2 = [1,2,0,2]
    expected_opt_first_state = 3
    expected_opt_length = 4
    
    assert_equal(expected_opt_first_state, opt_first_state, "While testing optimized first state : ")
    assert_equal(expected_opt_length, opt_ic.length, "While testing optimized token array size : ")
    assert_equal(expected_opt_length, opt_state1.length, "While testing optimized state1 array size : ")
    assert_equal(expected_opt_length, opt_state2.length, "While testing optimized state2 array size : ")
    assert_equal(expected_opt_ic, opt_ic, "While testing optimized token array contents : ")
    assert_equal(expected_opt_state1, opt_state1, "While testing optimized state1 array contents : ")
    assert_equal(expected_opt_state2, opt_state2, "While testing optimized state2 array contents : ")
    
  end

end
